package com.joysuch.wwyt.edu.repository;

import com.joysuch.wwyt.edu.entity.EduCourseLearnHistory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/9/11 11:50
 */
public interface EduCourseLearnHistoryDao extends JpaRepository<EduCourseLearnHistory, Long>, JpaSpecificationExecutor<EduCourseLearnHistory> {


    Page<EduCourseLearnHistory> findByUserId(Long userId, Pageable pageable);

    @Query(value = " select * from (select * from wwyt_edu_course_learn_history where user_id=?1 and  delete_flag='0' ORDER BY app_learn_time DESC limit 1000) as" +
            " t group by t.courseaware_id",nativeQuery = true)
    List<EduCourseLearnHistory> findHistoryByUserId(Long userId);


    /**
     * 查询用户观看课件进度
     * @param courseawareId
     * @param userId
     * @return
     */
    @Query(value = "select t.* from wwyt_edu_course_learn_history t where delete_flag='0' and t.courseaware_id=?1 and t.user_id=?2 order by t.id desc limit 1 ", nativeQuery = true)
    EduCourseLearnHistory findByCourseawareIdAndUserId(Long courseawareId, Long userId);

    @Query(value = "select count(1) from wwyt_edu_course_learn_history t where delete_flag='0' and t.courseaware_id=?1 and t.user_id=?2 and t.state = 1 ", nativeQuery = true)
    Integer countState(Long coursewareId, Long userId);

    @Query(value = "select count(*) from wwyt_edu_course_learn_history t where delete_flag='0' and t.courseaware_id=?1 and t.user_id=?2 and t.state=1 ", nativeQuery = true)
    Integer findByIdsAndState(Long videoIds, Long userId);

    @Query(value = "select count(*) from wwyt_edu_course_learn_history t where delete_flag='0' and t.courseaware_id=?1 and t.user_id=?2", nativeQuery = true)
    Integer countByCoursewareIdAndUserId(Long id, Long currentUserId);

}
